package KeTangLian;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.Scanner;

import javax.management.Query;
import javax.swing.text.html.HTMLDocument.HTMLReader.IsindexAction;
import javax.xml.transform.Templates;

public class DBManager {
	private static ResultSet resultSet;
	public static Connection conn() {
		//连接数据库MySQL
		try {
			Class.forName("com.mysql.jdbc.Driver");
			return DriverManager.getConnection("jdbc:mysql://localhost:3306/java十一章", "root", "123456");
		} catch (ClassNotFoundException e) {
			e.printStackTrace();
		} catch (SQLException e) {
			e.printStackTrace();
		}
		return null;
	}
public static int excuteUpdate(String sql) {//增删改
	Connection conn=null;
	Statement st=null;
	try {
		conn=conn();
		st=conn.createStatement();
		return st.executeUpdate(sql);
	}catch (SQLException e) {
		e.printStackTrace();
		closeALL(null, st, conn);
		return 0;
	}
	
}
public static QueryDataResult excuteQuery(String sql) {//查询
	Connection conn = null; 
	  Statement st = null; 
	  ResultSet rs = null; 
	  try { 
	   conn = conn(); 
	   st = conn.createStatement(); 
	   rs = st.executeQuery(sql); 
	   return new QueryDataResult(conn, st, rs); 
	  } catch (SQLException e) { 
	   e.printStackTrace(); 
	   closeALL(rs, st, conn); 
	   return null; 
	  }
}
public static void closeALL(ResultSet rs,Statement st,Connection conn) {
	try {
		if (conn!=null) {
			conn.close();
		}
        if (st!=null) {
        	st.close();
		}
        if (rs!=null) {
        	rs.close();
		}
	} catch (SQLException e) {
		e.printStackTrace();
	}
	
	
}
}
